AsciiMatrix
“Divide and Conquer.”
Popular Proverb
A challenging project
This is the last tutorial were we will be programming in textmode. I invented a game (based on a vague memory of a game I played as a child), with a story, and 3 levels. In the future you will have to accommodate for different levels, and presentation screens in your games (not to mention menus and stuff). These things are more complicated then they look (they are certainly outside the scope of this tutorial). Just look at how we need almost 300 lines of code to make a game as “simple” (for a non programmers view) as the next one. You've gained some experience with diluted problems. This one is almost as real world as we can get in basic4gl. The next series of lessons will incorporate 2d graphics and some simple artificial intelligence. But for now this problem is challenging enough.
Open up asciimatrix.gb with Basic4GL and run it. If you look a little bit at the code you will see that 90% of it is just showing text and waiting around. The real work is being done by the subroutine Play. In the file asciimatrixnoplay.gb I have deleted this subroutine and everything related to it. You can still run the program but, it's not a game. Your task is to code this subroutine, the real game. Play asciimatrix for a while to get a hang of what you have to do.
The specifics
This is how asciimatrix works. At the start of the program the subroutine Intro1 is called. This prints the introduction for the first level.
Then Setup1 is called. Setup1 sets the screen to a 40x30 textmode and prints the map. It also puts the players initial position in the x and y variables. The variables alive,win,energy,tick,idleticks,maxidleticks,xinc,yinc, and char$ are all set to correct values. Finaly Play is called. If play returns with alive set to false, the gameover message is displayed, and the player will restart the last level. If Play returns with alive set to true, the intro and setup are called for the next level. After three levels the game is won with a corresponding message displayed.
The subroutine Play must perform a series of turns as long as the alive variable is set to true and the win variable to false. The rate at which Play completes one turn, is set by the tick variable. Tick is the number of miliseconds a turn should last.
The variable idleticks is suplied to count the number of turns the player remains idle (adds no characters to the level). Remember the player loses if he is idle for more then maxidleticks turns. Also if he collides into a # character he should also lose. The variable energy is suplied to count how many $ characters are left on screen (the setup routine makes sure its at the correct value on setup). The player wins the level if he gets all $ characters. If the player loses the routine should return with alive set to false, if he wins alive should be true when Play returns.
Get the input from the keyboard, leftarrow prints a backslash at the players current position, rightarrow a forwardslash. The complication is that the player shouldn't be able to overwrite other characters except a space or a $. In the case he overwrites a $ character it should count as if he picked it up. If the player fails to add a character during this turn, the turn counts as idle (idleticks should be updated and checked accordingly).
Also the O should be moved around acording to xinc, yinc, and depending on if we are standing on a slash character. In such a case xinc and yinc should be updated acordingly and the player should bounce apropriatly. Complications arive since we have to be able to restore the character that was under our current position when we leave, the char$ variable is provided for this purpouse. Also the death condition should be checked.
Feel free to rename variables or add new variables. As well as doing things more efficiently. The only real requisite of this problem is that your play subroutine should make the output equal to what happens when you run asciimatrix.gb. And remember the quote of this chapter.